package com.databasedesign.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.databasedesign.dao.DormitoryDao;
import com.databasedesign.dao.DormitoryLiveinDao;
import com.databasedesign.dao.StudentDao;
import com.databasedesign.pojo.dto.StudentDTO;
import com.databasedesign.pojo.po.Student;
import com.databasedesign.service.DormitoryLiveinService;
import com.databasedesign.service.DormitoryService;
import com.databasedesign.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StudentServiceImpl extends ServiceImpl<StudentDao, Student> implements StudentService {
    @Autowired
    private StudentDao studentDao;

    @Autowired
    private DormitoryService dormitoryService;

    @Autowired
    private DormitoryLiveinDao dormitoryLiveinDao;

    public List<Student> GetAll(){
        return studentDao.GetAll();
    }

    @Override
    public Student GetByStudentId(String StudentId) {
        return studentDao.GetByStudentId(StudentId);
    }

    /**
     * 安排女生的入住顺序
     * @return
     */
    public List<Student> GetGirlAddress(){
        List<String> Addresslist = studentDao.GetGirlAddress();
        Integer addressnum = Addresslist.size();
        Map<String,Object> map = new HashMap<String, Object>();
        for (int i = 0; i < addressnum ; i++) {
            List<Student> list = new ArrayList<Student>();
            list = studentDao.GetGirlByAddress(Addresslist.get(i));
            map.put("list" + (i+1), list);
        }

        List<Student> livein = new ArrayList<Student>();
        Integer size = 0;
        int i =0;
        while(true) {
            for(Map.Entry<String,Object> entry:map.entrySet()){
                List<Student> list = (List<Student>) entry.getValue();
                if(i>list.size()-1){
                    continue;
                }
                Student student = list.get(i);
                livein.add(student);
            }
            i = i+1;
            if(livein.size()== size)
                break;
            size = livein.size();
        }
        return livein;
    }


    /**
     * 安排男生的入住顺序
     * @return
     */
    public List<Student> GetBoyAddress(){
        List<String> Addresslist = studentDao.GetBoyAddress();
        Integer addressnum = Addresslist.size();
        Map<String,Object> map = new HashMap<String, Object>();
        for (int i = 0; i < addressnum ; i++) {
            List<Student> list = new ArrayList<Student>();
            list = studentDao.GetBoyByAddress(Addresslist.get(i));
            map.put("list" + (i+1), list);
        }
        List<Student> livein = new ArrayList<Student>();
        Integer size = 0;
        int i =0;
        while(true) {
            for(Map.Entry<String,Object> entry:map.entrySet()){
                List<Student> list = (List<Student>) entry.getValue();
                if(i>list.size()-1){
                    continue;
                }
                Student student = list.get(i);
                livein.add(student);
            }
            i = i+1;
            if(livein.size()== size)
                break;
            size = livein.size();
        }
        return livein;
    }

    @Override
    public List<Student> GetByRoomNumber(String RoomNumber) {
        Integer DormitoryId =  dormitoryService.GetIdByRoomNumber(RoomNumber);
        List<String> studentNumlist = dormitoryLiveinDao.GetStudentNumByDormitoryId(DormitoryId);
        List<Student> studentList = new ArrayList<Student>();
        for(String StudentNum:studentNumlist){
            Student student = studentDao.GetByStudentId(StudentNum);
            studentList.add(student);
        }
        return studentList;
    }

    @Override
    public String GetByStudentNumber(String StudentNumber) {
        Integer DormitoryId = dormitoryLiveinDao.GetDorIdByStudentNumber(StudentNumber);
        return dormitoryService.GetRoomNumberById(DormitoryId);
    }

    @Override
    public List<StudentDTO> GetDormitoryByCollegeOrSubject(String CollegeOrSubject) {
        List<StudentDTO> studentDTOList = new ArrayList<StudentDTO>();
        List<Student> studentList = studentDao.GetStudentByCollegeOrSubject(CollegeOrSubject);
        for(Student student :studentList){
            StudentDTO studentDTO = new StudentDTO();
            BeanUtil.copyProperties(student,studentDTO);
            Integer DormitoryId = dormitoryLiveinDao.GetDorIdByStudentNumber(student.getStudentNumber());
            String RoomNum = dormitoryService.GetRoomNumberById(DormitoryId);
            studentDTO.setRoomNumber(RoomNum);
            studentDTOList.add(studentDTO);
        }
        return studentDTOList;
    }


}
